package xyz.xtgl.erp.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.core.util.CommonUtil;
import common.core.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import plus.logic.LogicBaseHelp;
import xyz.xtgl.erp.system.entity.SysUser;
import xyz.xtgl.erp.system.entity.help.SysUserHelp;
import xyz.xtgl.erp.system.service.ISysUserService;

import java.util.List;

/**
 * <p>
 * 基础表 前端控制器
 * </p>
 *
 * @author liubo
 * @since 2020-02-02
 */
@Slf4j
@Controller
@RequestMapping("/system/sys_user")
public class SysUserController {

    @Autowired
    private ISysUserService iSysUserService;

    @Autowired
    private PasswordEncoder passwordEncoder;

    /**
     * 跳转的列表页面
     * @return 跳转页面
     */
    @RequestMapping("/listPage")
    public Object listPage() {
      return "sysUser_list";
    }

    /**
     * 列表页面表格数据
     * @param query 查询条件
     * @param page 分页对象
     * @return page 分页对象
     */
    @ResponseBody
    @RequestMapping("/listData")
    public Object listData(QueryWrapper<SysUser> query, Page<SysUser> page) {
        return Result.success(iSysUserService.page(page, query));
    }

    /**
     * 跳转到修改页面
     * @param id 唯一主键
     * @param model Spring提供的绑定器
     * @return 修改页面
     */
    @RequestMapping("/editPage")
    public Object editPage(String id, Model model) {
        if(CommonUtil.isNotEmpty(id)) {
            SysUser entity = this.iSysUserService.getById(id);
            if(CommonUtil.isNotEmpty(entity)) {
                model.addAttribute("entity",entity);
            }
        }
        return "sysUser_edit";
    }

    /**
     * 数据保存
     * @param entity 实体类
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/save")
    public Object save(SysUser entity) {
        log.info("开始保存数据:{}" ,entity);
        QueryWrapper<SysUser> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(SysUserHelp.phone,entity.getPhone());
        if(CommonUtil.isEmpty(entity.getId())){
            entity.setPassword(passwordEncoder.encode("123456"));
            if(CommonUtil.isEmpty(entity.getPhone())){
                return Result.fail("电话号码不能为空");
            }
        }else{
            queryWrapper.ne(LogicBaseHelp.id,entity.getId());
            if(CommonUtil.isNotEmpty(entity.getPassword())){
                entity.setPassword(passwordEncoder.encode(entity.getPassword()));
            }
        }
        List<SysUser> list = iSysUserService.list(queryWrapper);
        if(CommonUtil.isNotEmpty(list)){
            return Result.fail("手机号码重复");
        }
        this.iSysUserService.saveOrUpdate(entity);
        return Result.success();
    }

    /**
     * 数据删除
     * @param queryWrapper 删除条件
     * @return Result 统一结果对象
     */
    @ResponseBody
    @RequestMapping("/removed")
    public Object removed(QueryWrapper<SysUser> queryWrapper) {
        this.iSysUserService.remove(queryWrapper);
        return Result.success("删除成功");
    }

}
